/**
 * Created by zhanglihe on 15/9/9.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'model',
    'dialog',
    'ckeditor',
    'validation'
], function ($, common, ready, template, Model, dialog, ckeditor, Validation) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.event();
            this.fetch();
        },

        fetch: function () {
            var _this = this;
            new Model({
                container: 'main-view',
                template: 'setting-tpl',
                tag_name: 'div',
                url: _global.url.api + 'site/' + id,
                callback: function () {
                    loaded();
                }
            }).fetch();
            $('[name="detail"]').ckeditor({toolbar: []});

            //表单验证
            this.validation = new Validation({
                rules: {
                    img: {required: true},
                    homepage_background: {required: true},
                    name: {required: true}
                }
            });

        },

        event: function () {
            this.data = {};
            var _this = this;
            console.log(_this);

            //上传logo
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();

                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            }).on('click', '#btn-upload-bg', function (e) { //上传背景
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.bgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });

            $("#btn-save").on("click", function () {

                if (_this.validation.isFail()) {
                    Alert({msg: '提交失败，请正确填写各项内容', type: 'error'});
                    return false;
                }

                _this.data.img = $("input[name='img']").val();
                _this.data.homepage_background = $('input[name="homepage_background"]').val();

                _this.data.name = $.trim($("#name").val());

                _this.data.short_name = $("#short_name").val();
                _this.data.order_expire_time = $("#order_expire_time").val();
                _this.data.order_mobile = $("#order_mobile").val();
                _this.data.kefu_mobile = $("#kefu_mobile").val();
                _this.data.rebate = $("#rebate").val();
                _this.data.intro = $("#intro").val();
                _this.data.media_content = $("#detail").val();
                _this.data.agency_id = $("#agency_id").val();
                _this.data.group_discount = $("#group_discount").val();
                _this.data.lmessage = $("#lmessage").val();
                if(new Model({
                        url: _global.url.api + 'site/'+id,
                        data: _this.data
                    }).put()) {
                }

            });
        },
        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;

                $('#img-view').html(template('img-tpl', img));
            }
        },

        bgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;

                $('#bg-view').html(template('bg-tpl', img));
            }
        },
        mobile: function (str) {
            var reg = /^(1[3|4|5|7|8][0-9]\d{8})$/;
            return reg.test(str);
        }

    };

    new index();

});